var http = require('../../utils/http')
Component({
  //向组件的传参
  properties:{
    item:Object
  },
  data: {
    comments: {},
    comment: {},
    msg: '',
    self: {},
    lookLikeFavourite:{}
  },
  //
  lifetimes:{
    
  },
  //生命周期
  pageLifetimes:{
    show(){
        this.findComment(this.properties.item);
        this.setData({
            self: wx.getStorageSync('userInfo'),
        })
        this.lookLikeFavourite("look")
    }
  },
  methods:{
    //获取评论
     findComment(item) {
        const that = this;
        this.setData({
            'comment.topicId': item.id,
            'comment.topicType': item.topicType
        })
        http.postNoHeader("/comment/findComment",that.data.comment,(res)=>{
            that.setData({
                comments: res.data.data,
                comment: {}
            })
        })
    
    },
     //回复对象
     reply(e) {
        this.setData({
            'comment.parentId': e.currentTarget.dataset.id,
            'comment.target': e.currentTarget.dataset.nickname
        })
    },
  
    //发表评论
    addComment() {
        const that = this;
        this.setData({
            'comment.content': this.data.msg,
            'comment.topicId': this.properties.item.id,
            'comment.commentUid': this.data.self.id,
            'comment.avatar': this.data.self.avatar,
            'comment.nickName': this.data.self.nickName,
            'comment.topicType': this.properties.item.topicType,
        })
        http.post("/comment/addComment",that.data.comment,(res)=>{
            that.findComment(that.properties.item)
            that.setData({
                comment: {},
                msg: ''
            })
        })
    },
    toExpertDetails(events) {
        let temp = events.currentTarget.dataset.item;
        console.log(temp);
        let item = {
            id:temp.commentUid,
            avatar:temp.avatar,
            nickName:temp.nickName
        }
        console.log(item);
        item = encodeURIComponent(JSON.stringify(item)) 
        wx.navigateTo({
            url: '/pages/expertDetails/expertDetails?item='+item
        })
    },
    bindfocus(){
        http.get("/mini/testToken","",res=>{
            if(res.statusCode == 401){
                wx.navigateTo({
                    url: '/pages/login1/login1'
                })
            }
        })
    },
    lookLikeFavourite(e){
        http.get("/mini/testToken","",res=>{
            if(res.statusCode == 401 && e !="look"){
                wx.navigateTo({
                    url: '/pages/login1/login1'
                })
            }else{
                if (e == "look") {
                    if(res.statusCode != 401){
                        this.setData({
                            'lookLikeFavourite.lookId':this.properties.item.id,
                            'lookLikeFavourite.type':this.properties.item.topicType
                        })
                    }
                }else if(e.currentTarget.dataset.flag == "favourite"){
                    this.setData({
                        'item.favourite': !this.properties.item.favourite,
                        'lookLikeFavourite.favouriteId':this.properties.item.id,
                        'lookLikeFavourite.type':this.properties.item.topicType
                    })
                    if (this.properties.item.favourite) {
                        this.setData({
                            'item.favouriteNum':this.properties.item.favouriteNum + 1
                        })
                    }else{
                        this.setData({
                            'item.favouriteNum':this.properties.item.favouriteNum - 1
                        })    
                    }
                   
                }else if(e.currentTarget.dataset.flag == "like"){
                    this.setData({
                        'item.like': !this.properties.item.like,
                        'lookLikeFavourite.likeId':this.properties.item.id,
                        'lookLikeFavourite.type':this.properties.item.topicType
                    })
        
                    if (this.properties.item.like) {
                        this.setData({
                            'item.likeNum':this.properties.item.likeNum + 1
                        })
                    }else{
                        this.setData({
                            'item.likeNum':this.properties.item.likeNum - 1
                        })    
                    }
                    this.triggerEvent("itemChange",this.data.item)
                }
                
                if(res.statusCode!=401){
                    http.post("/looklikefavourite/addLookLikeFavouriteNum",this.data.lookLikeFavourite,res=>{
                        console.log(res);
                        this.data.lookLikeFavourite = {}
                    })
                }
               
        
            }
        })
        
        

        
    }
  }
})